package com.example.kotlin_retrofit.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.kotlin_retrofit.R
import com.example.kotlin_retrofit.entity.User
import kotlinx.android.synthetic.main.item_user.view.*

class UserAdapter(var mList: List<User>) :
    RecyclerView.Adapter<UserAdapter.ViewHolder>() {
    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var mTvUser: TextView = view.tv_user
    }

    private var mUserClickListener: OnUserClickListener? = null

    interface OnUserClickListener {
        fun click(text: String?)
    }

    fun setOnUserClickListener(onUserClickListener: OnUserClickListener) {
        this.mUserClickListener = onUserClickListener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            LayoutInflater.from(parent.context).inflate(
                R.layout.item_user,
                parent,
                false
            )
        )
    }

    override fun getItemCount(): Int {
        return mList.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.mTvUser.text = mList[position].name
        holder.mTvUser.setOnClickListener {
            mUserClickListener!!.click(mList[position].name)
        }
    }

}